﻿using Jst.Application.Contracts.Dto.SignIn;
using Jst.Application.Contracts.IApplication.SignIn;
using Jst.Common;
using Jst.Core.jwt;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;

namespace Jst.EntityFrameworkCore.Controllers.SignIn
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthController : BaseApiController
    {
        private readonly IAuthServices _authServices;

        public AuthController(IAuthServices authServices)
        {
            _authServices = authServices;
        }

        /// <summary>
        /// 获取签名
        /// </summary>
        /// <param name="token">签名</param>
        /// <param name="secretKey">密钥</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetSignature([FromBody] DataParamAndModel input)
        {
            var result = input.Content.AesObjDecrypt<LoginParam>();
            return SUCCESSEncrypt(await _authServices.GetSignature(result.token, result.secretKey));
        }


        /// <summary>
        /// 获取jwt令牌
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetJwtToken([FromBody] DataParamAndModel input)
        {
            var result = input.Content.AesObjDecrypt<LoginInput>();
            return SUCCESSEncrypt(await _authServices.GetJwtToken(result));
        }

        /// <summary>
        /// 请求刷新Token（以旧换新）
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RefreshToken([FromBody] DataParamAndModel input)
        {
            var result = input.Content.AesObjDecrypt<string>();
            return SUCCESSEncrypt(await _authServices.RefreshToken(result));
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetLoginAsync([FromBody] DataParamAndModel input)
        {
            var result = input.Content.AesObjDecrypt<LoginInput>();

            var res = await _authServices.GetJwtToken(result);

            if (res.success)
            {
                var jsonString = JsonSerializer.Serialize(res.data);
                var model = JsonSerializer.Deserialize<JwtTokenResponseJson>(jsonString);

                var query = await _authServices.SingleLogin(model.Token, model.Expires_in);

                return SUCCESSEncrypt(query.data);
            }

            return SUCCESSEncrypt(res);
        }



        /// <summary>
        /// 菜单树
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetLoginMenue([FromBody] DataParamAndModel input)
        {
            var result = input.Content.AesObjDecrypt<LoginParam>();
            var res = await _authServices.GetLoginMenue(result.token);
            return SUCCESSEncrypt(res);
        }
    }

}
